package com.ruyiadmin.springboot.common_infrastructure.models;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.List;

/**
 * <p>
 * RuYiAdmin统一查询结果
 * </p>
 *
 * @author RuYiAdmin
 * @since 2022-07-14
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class QueryResponseResult<T> extends RuYiAdminBaseDomainEntity {

    //Http状态码
    private int httpStatusCode;
    //返回消息
    private String message;
    //查询数据总数
    private long totalCount;
    //查询记录
    private List<T> list;

    //region 反序列化方法

    /**
     * <p>
     * 反序列化方法
     * </p>
     *
     * @param text 字符串
     * @param <T>  泛型
     * @return QueryResult
     */
    public static <T> QueryResponseResult<T> deserializeObject(String text) {
        return JSON.parseObject(text, new TypeReference<QueryResponseResult<T>>() {
        });
    }

    //endregion

    public static <T> QueryResponseResult<T> success(long totalCount, List<T> list) {
        return new QueryResponseResult<>(HttpStatus.OK.value(), "OK", totalCount, list);
    }

    public static <T> QueryResponseResult<T> success(List<T> list) {
        return new QueryResponseResult<>(HttpStatus.OK.value(), "OK", 0, list);
    }

    public static <T> QueryResponseResult<T> ok(long totalCount, List<T> list) {
        return new QueryResponseResult<>(HttpStatus.OK.value(), "OK", totalCount, list);
    }

    public static <T> QueryResponseResult<T> ok(List<T> list) {
        return new QueryResponseResult<>(HttpStatus.OK.value(), "OK", 0, list);
    }

    public static <T> QueryResponseResult<T> error() {
        return new QueryResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "ERROR", 0, null);
    }

    public static <T> QueryResponseResult<T> error(String message) {
        return new QueryResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> badRequest() {
        return new QueryResponseResult<>(HttpStatus.BAD_REQUEST.value(), "BAD_REQUEST", 0, null);
    }

    public static <T> QueryResponseResult<T> badRequest(String message) {
        return new QueryResponseResult<>(HttpStatus.BAD_REQUEST.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> unauthorized() {
        return new QueryResponseResult<>(HttpStatus.UNAUTHORIZED.value(), "UNAUTHORIZED", 0, null);
    }

    public static <T> QueryResponseResult<T> unauthorized(String message) {
        return new QueryResponseResult<>(HttpStatus.UNAUTHORIZED.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> forbidden() {
        return new QueryResponseResult<>(HttpStatus.FORBIDDEN.value(), "FORBIDDEN", 0, null);
    }

    public static <T> QueryResponseResult<T> forbidden(String message) {
        return new QueryResponseResult<>(HttpStatus.FORBIDDEN.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> notFound() {
        return new QueryResponseResult<>(HttpStatus.NOT_FOUND.value(), "NOT_FOUND", 0, null);
    }

    public static <T> QueryResponseResult<T> notFound(String message) {
        return new QueryResponseResult<>(HttpStatus.NOT_FOUND.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> noContent() {
        return new QueryResponseResult<>(HttpStatus.NO_CONTENT.value(), "NO_CONTENT", 0, null);
    }

    public static <T> QueryResponseResult<T> noContent(String message) {
        return new QueryResponseResult<>(HttpStatus.NO_CONTENT.value(), message, 0, null);
    }

    public static <T> QueryResponseResult<T> non(List<T> list) {
        return new QueryResponseResult<>(HttpStatus.OK.value(), "OK", 0, list);
    }

}
